package com.amigo.online.provider.video.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.amigo.online.common.base.controller.BaseController;
import com.amigo.online.common.core.response.Response;
import com.amigo.online.common.core.response.StatusCode;
import com.amigo.online.common.core.security.IgnoreSecurity;
import com.amigo.online.common.param.dto.video.MusicDto;
import com.amigo.online.common.param.result.MusicInfo;
import com.amigo.online.common.util.CommonValidator;
import com.amigo.online.provider.video.common.GlobalController;
import com.amigo.online.provider.video.exception.MusicException;
import com.amigo.online.provider.video.exception.VideoException;
import com.amigo.online.provider.video.model.entity.Music;
import com.amigo.online.provider.video.service.MusicService;
import com.amigo.online.provider.video.util.ConvertUtil;

/**
 * 音频相关
 *
 * @author amigo
 */
@RestController
@RequestMapping(value = "/music", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class MusicController extends BaseController {

    @Resource
    private MusicService musicService;

    /**
     * 音乐列表
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/system/musics/{musicid}/{page}/{size}")
    public ResponseEntity<Response> myVideos(@PathVariable Long musicid, @PathVariable Integer page, @PathVariable Integer size) {
        if (musicid == null) {
            throw new MusicException(StatusCode.BAD_REQUEST);
        }
        page = GlobalController.checkPage(page, size);
        List<MusicInfo> musicList = musicService.getMusics(musicid, page, size);
        return success(musicList);
    }

    /**
     * 我收藏的音乐列表
     *
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/my/collect/musics/{page}/{size}")
    public ResponseEntity<Response> myUpedVideos(@PathVariable Integer page, @PathVariable Integer size) {
        page = GlobalController.checkPage(page, size);
        List<MusicInfo> videoList = musicService.getMyCollectMusics(page, size);
        return success(videoList);
    }

    /**
     * 收藏音乐
     *
     * @return
     */
    @GetMapping(value = "/collect/music/{musicid}")
    public ResponseEntity<Response> collectMusic(@PathVariable Long musicid) {
        if (musicid == null) {
            throw new MusicException(StatusCode.BAD_REQUEST);
        }
        boolean flag = musicService.collectMusic(musicid);
        return success(flag);
    }

    /**
     * 取消收藏
     */
    @DeleteMapping(value = "/cancel/collect/{musicid}")
    public ResponseEntity<Response> cancelCollect(@PathVariable Long musicid) {
        if (musicid == null) {
            throw new MusicException(StatusCode.BAD_REQUEST);
        }
        boolean flag = musicService.cancelCollect(musicid);
        return success(flag);
    }

    /**
     * 获取全部数据
     */
    @GetMapping(value = "/getAll")
    public ResponseEntity<Response> getAll() {
        List<Music> videoList = musicService.getAll();
        return success(videoList);
    }

    /**
     * 新增音乐
     */
    @PostMapping(value = "/new")
    public ResponseEntity<Response> newUserVideo(@RequestBody MusicDto dto) {
        if (!CommonValidator.validate(dto)) {
            throw new MusicException(StatusCode.BAD_REQUEST);
        }
        MusicInfo music = musicService.newMusic(dto);
        return success(music);
    }

    /**
     * 根据id查询单条数据
     */
    @GetMapping(value = "/music/{musicid}")
    public ResponseEntity<Response> getMusicById(@PathVariable Long musicid) {
        Music music = musicService.getMusicById(musicid);
        return success(music);
    }

    /**
     * 根据musicids查询music信息
     */
    @IgnoreSecurity
    @GetMapping(value = "/getMusicByIds")
    public ResponseEntity<Response> getMusicByIds(@RequestParam(value = "ids", required = false) List<Long> ids) {
        List<Music> musics = musicService.getMusicByIds(ids);
        return success(ConvertUtil.musicsToMusicsInfo(musics));
    }

    /**
     * @param videoId
     * @param musicUrl
     * @return
     */
    @IgnoreSecurity
    @PostMapping(value = "/update/MusicOrginalMusic")
    public ResponseEntity<Response> updateMusicOrginalMusicUrl(@RequestParam("id") Long videoId, @RequestParam("userId") Long userId,
                                                               @RequestParam("url") String musicUrl, @RequestParam("duration") Integer duration) {
        if (videoId == null || videoId <= 0 || StringUtils.isBlank(musicUrl) || musicUrl.length() < 30 || duration < 0) {
            throw new VideoException(StatusCode.BAD_REQUEST);
        }
        return success(musicService.updateMusicOrginalMusicUrl(videoId, userId, musicUrl));
    }
}
